package com.shujia.flink.tf;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo1Map {
    public static void main(String[] args) throws Exception {
        //创建flink的环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //读取数据
        DataStream<String> studentsDS = env.readTextFile("flink/data/students.txt");

        //1、使用类的方式
        DataStream<Integer> agesDS = studentsDS.map(new AgeMapFunction());

        //agesDS.print();

        //2、使用匿名内部类的方式
        DataStream<String> clazzDS = studentsDS.map(new MapFunction<String, String>() {
            /**
             * map方法每一条数据都行执行一次
             */
            @Override
            public String map(String value) throws Exception {
                return value.split(",")[4];
            }
        });

        //clazzDS.print();

        //lambda表达式
        DataStream<String> namesDS = studentsDS.map(stu -> stu.split(",")[1]);

        namesDS.print();

        env.execute();
    }
}
